package com.nibianplus.volley;

import android.text.TextUtils;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.nibianplus.util.ContextUtil;
import com.nibianplus.volley.request.CommonFormRequest;
import com.nibianplus.volley.request.CommonFormRequest2;
import com.nibianplus.volley.request.CommonGetRequest;
import com.nibianplus.volley.request.CommonPostRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class VolleyHttpClient {

	private static final RequestQueue requestQueue = Volley.newRequestQueue(ContextUtil.getContext());

	public static void cancalAll(Object tag) {
		if (requestQueue != null) {
			requestQueue.cancelAll(tag);
		}
	}

	public static void get(final String url, final Map<String, String> map,
						   final JSONResponseCallback serviceResponseCallback, final int what, VolleyConfig internetConfig) {

		String fullUrl = url;
		if (map != null) {
			if (url.indexOf("?") == -1) {
				fullUrl = url + "?";
			}
			for (Map.Entry<String, String> entry : map.entrySet()) {
				try {
					fullUrl = fullUrl + URLEncoder.encode(entry.getKey(), "UTF-8") + "="
							+ URLEncoder.encode(entry.getValue(), "UTF-8") + "&";
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
					if (serviceResponseCallback != null) {
						JSONObject message = null;
						try {
							message = new JSONObject("{\"error\":"+e.getMessage()+"}");
						} catch (JSONException e1) {
							e1.printStackTrace();
						}
						serviceResponseCallback.error(what, message);
					}
				}
			}
			fullUrl = fullUrl.substring(0, fullUrl.length() - 1);
		}
		CommonGetRequest commonGetRequest = new CommonGetRequest(fullUrl, new Listener<JSONObject>() {

			@Override
			public void onResponse(JSONObject response) {
				printResult(url, response);

				if (serviceResponseCallback != null) {
					parseResponse(serviceResponseCallback, what, response);
				}

			}

		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				if (serviceResponseCallback != null) {
					JSONObject message = null;
					try {
						message = new JSONObject("{\"error\":"+error.getMessage()+"}");
					} catch (JSONException e1) {
						e1.printStackTrace();
					}
					serviceResponseCallback.error(what, message);
				}
			}
		});

		if (internetConfig != null) {
			commonGetRequest.setShouldCache(internetConfig.isShouldCache());

			if (!TextUtils.isEmpty(internetConfig.getTag())) {
				commonGetRequest.setTag(internetConfig.getTag());
			}

		}

		requestQueue.add(commonGetRequest);

	}

	public static void post(String url, Map<String, Object> map,JSONResponseCallback serviceResponseCallback, int what,
							VolleyConfig internetConfig) {
		List<JSONResponseCallback> callbacks = new ArrayList<com.nibianplus.volley.JSONResponseCallback>();
		if (serviceResponseCallback != null) {
			callbacks.add(serviceResponseCallback);
		}

		post(url, map, callbacks, what, internetConfig);

	}

	@Deprecated
	public static <T extends JSONResponseCallback> void post(final String url, final Map<String, Object> map,
																				 final List<T> serviceResponses, final int what, VolleyConfig internetConfig) {

		CommonPostRequest commonPostRequest = new CommonPostRequest(url, map, new Listener<JSONObject>() {

			@Override
			public void onResponse(JSONObject response) {
				String param = map == null ? "{}" : new JSONObject(map).toString();
				String name = url + param;
				printResult(name, response);

				for (JSONResponseCallback serviceResponseCallback : serviceResponses) {
					if (serviceResponseCallback != null) {
						parseResponse(serviceResponseCallback, what, response);
					}
				}

			}

		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				for (JSONResponseCallback serviceResponseCallback : serviceResponses) {
					if (serviceResponseCallback != null) {
						JSONObject message = null;
						try {
							message = new JSONObject("{\"error\":"+error.getStackTrace().toString()+"}");
						} catch (JSONException e1) {
							e1.printStackTrace();
						}
						serviceResponseCallback.error(what, message);
					}
				}
			}
		});

		if (internetConfig != null) {
			commonPostRequest.setShouldCache(internetConfig.isShouldCache());

			if (!TextUtils.isEmpty(internetConfig.getTag())) {
				commonPostRequest.setTag(internetConfig.getTag());
			}

		}

		requestQueue.add(commonPostRequest);
	}

	public static void form(final String url, final Map<String, Object> map, final Map<String, File> files,
							final JSONResponseCallback serviceResponseCallback, final int what) {

		CommonFormRequest commonFormRequest = new CommonFormRequest(url, map, files, new Listener<JSONObject>() {

			@Override
			public void onResponse(JSONObject response) {
				printResult(url, response);

				if (serviceResponseCallback != null) {
					parseResponse(serviceResponseCallback, what, response);
				}

			}

		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				if (serviceResponseCallback != null) {
					JSONObject message = null;
					try {
						message = new JSONObject("{\"error\":"+error.getMessage()+"}");
					} catch (JSONException e1) {
						e1.printStackTrace();
					}
					serviceResponseCallback.error(what, message);
				}
			}
		});

		requestQueue.add(commonFormRequest);

	}
	
	public static void form2(final String url, final Map<String, Object> map, final Map<String, List<File>> files,
							 final JSONResponseCallback serviceResponseCallback, final int what) {

		CommonFormRequest2 commonFormRequest = new CommonFormRequest2(url, map, files, new Listener<JSONObject>() {

			@Override
			public void onResponse(JSONObject response) {
				printResult(url, response);

				if (serviceResponseCallback != null) {
					parseResponse(serviceResponseCallback, what, response);
				}

			}

		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				if (serviceResponseCallback != null) {
					JSONObject message = null;
					try {
						message = new JSONObject("{\"error\":"+error.getMessage()+"}");
					} catch (JSONException e1) {
						e1.printStackTrace();
					}
					serviceResponseCallback.error(what,message);
				}
			}
		});

		requestQueue.add(commonFormRequest);

	}
	

	private static void parseResponse(JSONResponseCallback serviceResponseCallback, int what, JSONObject response) {

		serviceResponseCallback.parseResponse(what, response);

	}

	private static void printResult(String name, JSONObject response) {
		Log.i("VolleyHttpClient", name + " ===== " + response.toString());
	}

}
